package com.tfjybj.itoo.exam.message;

import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.*;

/**
 * @author 商俊帅
 * @date 2022/01/10 14:57
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class WorkPlace {

    private String workDate;

    private String workAddress;

    private String workAddressUrl;

    private String type;

    private String detail;

    private String detailUrl;

    @Data
    @Builder
    static class WorkPlaceMap{
        private String content;
        private String contentUrl;

        public static List<List<WorkPlaceMap>> toLists(){
            List<WorkPlace> workPlaces = toList();

            List<WorkPlaceMap> addressList=new ArrayList<>();
            List<WorkPlaceMap> typeList = new ArrayList<>();
            List<WorkPlaceMap> detailList = new ArrayList<>();
            //取出work及url
            for (WorkPlace workPlace:workPlaces){
                WorkPlaceMap address = WorkPlaceMap.builder()
                        .content(workPlace.getWorkAddress())
                        .contentUrl(workPlace.getWorkAddressUrl()).build();
                addressList.add(address);

                WorkPlaceMap type = WorkPlaceMap.builder()
                        .content(workPlace.getType())
                        .build();
                typeList.add(type);

                WorkPlaceMap detail = WorkPlaceMap.builder()
                        .content(workPlace.getDetail())
                        .contentUrl(workPlace.getDetailUrl()).build();
                detailList.add(detail);
            }
            List<List<WorkPlaceMap>> lists = Lists.newArrayList(addressList, typeList, detailList);
            return lists;
        }
    }

    private static List<WorkPlace> toList(){
        WorkPlace workPlace1 = WorkPlace.builder()
                .workDate("星期一")
                .workAddress("北京-东区-A10")
                .workAddressUrl("https://www.baidu.com/")
                .type("固定工位")
                .detail("查看位置")
                .detailUrl("https://www.baidu.com/").build();

        WorkPlace workPlace2 = WorkPlace.builder()
                .workDate("星期二")
                .workAddress("上海-东区-A10")
                .type("系统安排")
                .detail("修改/取消")
                .detailUrl("https://www.baidu.com/").build();

        WorkPlace workPlace3 = WorkPlace.builder()
                .workDate("星期三")
                .workAddress("东区-A10")
                .type("自主预定")
                .detail("修改/取消")
                .detailUrl("https://www.baidu.com/").build();

        WorkPlace workPlace4 = WorkPlace.builder()
                .workDate("星期四")
                .detail("预定")
                .detailUrl("https://www.baidu.com/").build();

        WorkPlace workPlace5 = WorkPlace.builder()
                .workDate("星期五")
                .workAddress("上海-东区-A10")
                .type("系统安排")
                .detail("修改/取消")
                .detailUrl("https://www.baidu.com/").build();

        WorkPlace workPlace6 = WorkPlace.builder().workDate("星期六").build();
        WorkPlace workPlace7 = WorkPlace.builder().workDate("星期日").build();

        List<WorkPlace> workPlaces = Lists.newArrayList(workPlace1, workPlace2, workPlace3, workPlace4, workPlace5, workPlace6, workPlace7);
        return workPlaces;
    }

    public static String getExcelHtml(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("\r\n");

        stringBuilder.append("<html><head></head><body><h2>工位安排表</h2>");
        stringBuilder.append("<table border=\"5\" style=\"border:solid 1px #E8F2F9;font-size=14px;;font-size:22px;\">");

        stringBuilder.append("<tr style=\"background-color: #428BCA; color:#ffffff\">" +
                "<th width=\"180px\">星期一</th>" +
                "<th width=\"100px\">星期二</th>" +
                "<th width=\"80px\">星期三</th>" +
                "<th width=\"80px\">星期四</th>" +
                "<th width=\"80px\">星期五</th>" +
                "<th width=\"80px\">星期六</th>" +
                "<th width=\"80px\">星期日</th>" +
                "</tr>");
        List<List<WorkPlaceMap>> lists = WorkPlaceMap.toLists();
        for (List<WorkPlaceMap> workPlaceMaps:lists){
            stringBuilder.append("</tr>");
            for (WorkPlaceMap workPlaceMap:workPlaceMaps){
                if (Objects.nonNull(workPlaceMap.getContentUrl())){
                    stringBuilder.append("<td style=\"text-align:center font-size:3pt\">")
                            .append("<object><a href='"+workPlaceMap.getContentUrl()+"' target='_blank'>").append(workPlaceMap.getContent())
                            .append("</a></object>")
                            .append("</td>");
                }else if(Objects.isNull(workPlaceMap.getContent())){
                    stringBuilder.append("<td style=\"text-align:center font-size:3pt\">" +""+ "</td>");
                }
                else {
                    stringBuilder.append("<td style=\"text-align:center font-size:3pt\">" +workPlaceMap.getContent() + "</td>");
                }
            }
            stringBuilder.append("</tr>");
        }
        stringBuilder.append("</table>");
        stringBuilder.append("</body></html>");

        String htmlBody = stringBuilder.toString();
        System.out.println(htmlBody);
        return htmlBody;
    }

    public static void main(String[] args) {

        String excelHtml = getExcelHtml();

    }
}
